using System;
using MiniExcelLibs;

namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<Person> persons = new List<Person>();

            Thread th = new Thread(new ThreadStart(() =>
            {
                for (int i = 0; i < 10000; i++)
                {
                    persons.Add(new Person() { Name = "Tom" + i, Age = 25 });
                    persons.Add(new Person() { Name = "Jerry" + i, Age = 30 });
                    persons.Add(new Person() { Name = "Mary" + i, Age = 28 });
                    this.Invoke(new Action(() => {

                        this.textBox1.Text += "写入Excel中..." + i + "\r\n";
                        textBox1.SelectionStart = textBox1.Text.Length;
                        this.textBox1.ScrollToCaret();
                    }));
                    this.Invoke(new Action(() => {
                        this.progressBar1.Maximum = 10000;
                        this.progressBar1.Value = i;
                    }));

                }
            }));
            th.Start();

            //var memory = new MemoryStream();
            //Task.Run(() =>
            //{
            //    for (int i = 0; i < 10000; i++)
            //    {
            //        persons.Add(new Person() { Name = "Tom" + i, Age = 25 });
            //        persons.Add(new Person() { Name = "Jerry" + i, Age = 30 });
            //        persons.Add(new Person() { Name = "Mary" + i, Age = 28 });
            //        this.Invoke(new Action(() => {

            //            this.textBox1.Text += "写入Excel中..." + i + "\r\n";
            //            textBox1.SelectionStart = textBox1.Text.Length;
            //            this.textBox1.ScrollToCaret();
            //            }));
            //        this.Invoke(new Action(() => {
            //            this.progressBar1.Maximum = 10000;
            //            this.progressBar1.Value = i;
            //        }));
                    
            //    }
            //});
            

            //memory.SaveAs(persons);
            //memory.Seek(0, SeekOrigin.Begin);
            MiniExcel.SaveAs("D:\\Persons.xlsx", persons);
            //开始异步自动线程池
            //await Task.Run(() =>
            //{

                

            //});
            //return File(memory, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Persons.xlsx");
        }
    }
    public record Person()
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}
